package de.slothsoft.commons.swing.window;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.slothsoft.commons.res.CommonImages;
import de.slothsoft.commons.res.CommonMessages;
import de.slothsoft.commons.swing.AboutPanel.AboutInfo;
import de.slothsoft.commons.swing.JToolBarManager;
import de.slothsoft.commons.swing.util.WindowUtil;

public abstract class AbstractMainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	public void open() {
		createMainPanel();
		internalSetVisible(true);
	}

	/**
	 * Use open() instead.
	 */

	@Override
	@Deprecated
	public void setVisible(boolean b) {
		if (b) {
			open();
		} else {
			internalSetVisible(b);
		}
	}

	protected void internalSetVisible(boolean b) {
		super.setVisible(b);
	}

	private void createMainPanel() {
		setLayout(new BorderLayout());

		Component toolbar = createToolbar();
		if (toolbar != null) {
			add(toolbar, BorderLayout.NORTH);
		}

		Component component = createComponents();
		if (component != null) {
			add(component, BorderLayout.CENTER);
		}

		Dimension size = getInitialSize();
		if (size != null) {
			setSize(size);
		}
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		WindowUtil.centerWindow(this);
	}

	protected final Component createToolbar() {
		if (!hasToolbar() && !hasAboutToolbar())
			return null;

		JPanel parent = new JPanel();
		parent.setLayout(new BorderLayout());

		if (hasToolbar()) {
			JToolBarManager manager = new JToolBarManager();
			manager.getToolBar().setFloatable(false);
			createToolbar(manager);
			parent.add(manager.getToolBar(), BorderLayout.CENTER);
		} else {
			parent.add(new JLabel(), BorderLayout.CENTER);
		}

		if (hasAboutToolbar()) {
			JToolBarManager aboutManager = new JToolBarManager();
			aboutManager.getToolBar().setFloatable(false);
			aboutManager
					.add(CommonImages.get(CommonImages.TOOLBAR_ABOUT),
							CommonMessages
									.getString("AboutDialog.toolbarButton"), new ActionListener() {//$NON-NLS-1$

								@Override
								public void actionPerformed(ActionEvent e) {
									performAbout();
								}
							});
			parent.add(aboutManager.getToolBar(), BorderLayout.EAST);
		}

		return parent;
	}

	protected void performAbout() {
		AboutDialog dialog = new AboutDialog(this, createAboutInfo());
		dialog.open();
	}

	protected AboutInfo createAboutInfo() {
		return null; // to be overridden
	}

	protected boolean hasAboutToolbar() {
		return false; // to be overridden
	}

	protected boolean hasToolbar() {
		return false; // to be overridden
	}

	protected void createToolbar(JToolBarManager manager) {
		// to be overridden
	}

	// must be overriden

	protected abstract Component createComponents();

	public Dimension getInitialSize() {
		return new Dimension(800, 600);
	}
}
